package com.cardbaobao.cardbabyclient.ui.activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.api.Api;
import com.cardbaobao.cardbabyclient.buried.Google;
import com.cardbaobao.cardbabyclient.buried.Self;
import com.cardbaobao.cardbabyclient.buried.SelfUtils;
import com.cardbaobao.cardbabyclient.ui.activity.base.BaseActivity;
import com.cardbaobao.cardbabyclient.base.Constant;
import com.cardbaobao.cardbabyclient.utils.ApplicationHelper;
import com.cardbaobao.cardbabyclient.buried.GoogleAnalyticsUtil;
import com.cardbaobao.cardbabyclient.utils.ToastUtil;
import com.cardbaobao.cardbabyclient.utils.UmengEventUtils;
import com.cardbaobao.cardbabyclient.widget.ChangeTextView;
import com.cardbaobao.cardbabyclient.widget.TitleBar;
import com.gyf.barlibrary.ImmersionBar;

import butterknife.BindView;
import rx.Observable;
import rx.Subscriber;

/**
 * Created by Administrator on 2016/12/29.
 * 问题反馈页面
 */
public class FeedbackActivity extends BaseActivity {
    @BindView(R.id.title_bar)
    TitleBar titleBar;
    @BindView(R.id.feedback_content)
    EditText feedback_content;//反馈的内容
    @BindView(R.id.contact_number)
    EditText contact_number;//联系方式
    @BindView(R.id.content_number)
    TextView content_number;//输入的字体
    @BindView(R.id.commit_data)
    ChangeTextView commit_data;//提交

    @Override
    protected void initView(Bundle savedInstanceState) {
        ImmersionBar.with(this).statusBarDarkFont(true,Constant.STATUS_BAR_FONT_ALPHA).keyboardEnable(true).init();
        titleBar.setLeftViewOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mCurrentPageId= Self.Page.app_feedback_page;
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        feedback_content.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String s = feedback_content.getText().toString().trim();
                content_number.setText(s.length() + "/500");
            }
        });

        commit_data.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                submitFeedBack();
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_feedback;
    }

    @Override
    protected void onResume() {
        super.onResume();
        UmengEventUtils.pageEvent(mContext,
                ApplicationHelper.getApplicationContext().getString(R.string.event_0x025));
        GoogleAnalyticsUtil.setScreenName(Google.FEED_BACK);
    }

    //提交
    private void submitFeedBack() {
        String feedBackContent = feedback_content.getText().toString().trim();
        String msg = "";
        if (feedBackContent.length() > 0) {
            String contactNumber = contact_number.getText().toString().trim();
            if (contactNumber.length() > 0) {
                SelfUtils.buriedClickData(Self.Action.app_click_feedback,
                        Self.Action.app_click_feedback_des,
                        mEnterPageTime,
                        mCurrentPageId,
                        null);
                submitFeedBackData(feedBackContent, contactNumber);
            } else {
                msg = "请填写您的联系方式,好让我们工作人员联系您";
            }
        } else {
            msg = "请输入您要反馈的问题";
        }
        if (!TextUtils.isEmpty(msg)) {
            ToastUtil.showToast( msg);
        }
    }

    /**
     * 意见反馈
     *
     * @param content
     * @param contact
     */
    private void submitFeedBackData(String content, String contact) {
        showProgressDialog(false);

        String nickName=null;
        if (mApplication.getIsLogin() && mApplication.getUser() != null) {
            nickName= mApplication.getUser().getUserName();
        }
        Observable<String> observable=
                Api.feedback(content,contact,nickName);
        observable.subscribe(new Subscriber<String>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                dismissProgressDialog();
                ToastUtil.showToast(ApplicationHelper.
                        getApplicationContext().
                        getString(R.string.string_request_server_fail));
            }

            @Override
            public void onNext(String s) {
                dismissProgressDialog();
                JSONObject object = JSON.parseObject(s);
                if (object.getIntValue("code") == 1) {
                    feedback_content.setText("");
                    contact_number.setText("");
                }
                ToastUtil.showToast("成功提交，感谢您的反馈");
            }
        });
    }

    @Override
    public void showProgressDialog(boolean isTouchHide) {
        commit_data.setChangeText("...");
        commit_data.startShowChangeTxt("正在提交");
        feedback_content.setEnabled(false);
        contact_number.setEnabled(false);
    }

    @Override
    public void dismissProgressDialog() {
        commit_data.stopShowChangeTxt();
        feedback_content.setEnabled(true);
        contact_number.setEnabled(true);
    }
}
